﻿using Analysis.Models.EchartModel;
using Bll;
using Entity.BBC;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Utils;

namespace Analysis.Controllers
{
    public class CountController : Controller
    {
        // GET: Count
        public ActionResult Index()
        {
            // 判断登录
            //if (Session["User"] == null)
            //{
            //    return RedirectToAction("Index", "LoginManager");
            //}
            String sqlStr = @"
                SELECT 
                    ProType, ProAddress,
	                (SELECT ValueDic FROM DataDiction WHERE ID=ProType) Str_ProType,
	                (SELECT ValueDic FROM DataDiction WHERE ID=ProAddress) Str_ProAddress, Varieti,
	                (SELECT TOP 1 PurPrice FROM PruData A WHERE A.ProType=T.ProType AND A.ProAddress=T.ProAddress AND A.Varieti=T.Varieti ORDER BY ShelfTime DESC) PurPrice,
	                (SELECT TOP 1 RetailPrice FROM PruData A WHERE A.ProType=T.ProType AND A.ProAddress=T.ProAddress AND A.Varieti=T.Varieti ORDER BY ShelfTime DESC) RetailPrice
                 FROM (
	                SELECT ProType, ProAddress, Varieti FROM PruData GROUP BY ProType, ProAddress, Varieti
                ) AS T";
            IList<SelectPruDataTable> SelProData = BllBBCCommon.SelectEntityBySql<SelectPruDataTable>("SelectPruDataTable", sqlStr);
            ViewData["SelProData"] = SelProData;

            return View();
        }

        public class SelectPruDataTable
        {
            public int ProType { get; set; }
            public int ProAddress { get; set; }
            public string Str_ProType { get; set; }
            public string Str_ProAddress { get; set; }
            public string Varieti { get; set; }
            public decimal PurPrice { get; set; }
            public decimal RetailPrice { get; set; }
        }

        public ActionResult GroupInfo(String ParamStr)
        {
            String[] Params = ParamStr.Split('|');
            String tproType = Params[0];
            String tvarieti = Params[1];
            String tproAddress = Params[2];
            
            DataDiction DDiction = BllBBCCommon.SelectEntity<DataDiction>("DataDiction", long.Parse(tproType));
            ViewData["tproType"] = DDiction.ValueDic;
            DDiction = BllBBCCommon.SelectEntity<DataDiction>("DataDiction", long.Parse(tproAddress));
            ViewData["tproAddress"] = DDiction.ValueDic;
            ViewData["tvarieti"] = tvarieti;

            return View();
        }

        public JsonResult GetPruDataTable(String paramStr)
        {
            Result<BarOrLineModelAndOtherData> result = new Result<BarOrLineModelAndOtherData>();

            List<string> legendList = new List<string>();
            List<string> nameList = new List<string>();
            List<List<decimal>> dataList = new List<List<decimal>>();

            legendList.Add("采购价");
            legendList.Add("零售价");

            List<decimal> buyPriceList = new List<decimal>();
            List<decimal> sellPriceList = new List<decimal>();

            String[] Params = paramStr.Split('|');
            String tproType = Params[0];
            String tvarieti = Params[1];
            String tproAddress = Params[2];

            String sqlStr = @"SELECT Id, ShelfTime, Grade, 
	            (SELECT ValueDic FROM DataDiction WHERE DataDiction.ID=Grade) Str_Grade, PurPrice, RetailPrice
                FROM PruData WHERE ProType='" + tproType + "' AND ProAddress='" + tproAddress + "' AND Varieti='" + tvarieti + "' ORDER BY ShelfTime DESC";
            IList<SelectPruDataTable2> SelPruData = BllBBCCommon.SelectEntityBySql<SelectPruDataTable2>("SelectPruDataTable2", sqlStr);

            foreach (SelectPruDataTable2 obj in SelPruData)
            {
                nameList.Add(obj.ShelfTime.ToString("yyyy-MM-dd"));
                buyPriceList.Add(obj.PurPrice);
                sellPriceList.Add(obj.RetailPrice);
            }
            dataList.Add(buyPriceList);
            dataList.Add(sellPriceList);


            BarOrLineModel bar = new BarOrLineModel();
            bar.NameList = nameList;
            bar.LegendList = legendList;
            bar.DataList = dataList;

            BarOrLineModelAndOtherData data = new BarOrLineModelAndOtherData();
            data.bar = bar;
            data.list = SelPruData.ToList();
           
            result.Status = 200;
            result.Body = data;


            return Json(result, JsonRequestBehavior.AllowGet);
        }

        public class BarOrLineModelAndOtherData
        {
            public BarOrLineModel bar { get; set; }

            public List<SelectPruDataTable2> list { get; set; }
        }

        public class SelectPruDataTable2
        {
            public long Id { get; set; }
            public DateTime ShelfTime { get; set; }
            public string ShelfTimeStr
            {
                get
                {
                    return ShelfTime.ToString("yyyy-MM-dd");
                }
            }
            public int Grade { get; set; }
            public string Str_Grade { get; set; }
            public decimal PurPrice { get; set; }
            public decimal RetailPrice { get; set; }
        }

        public ActionResult Detail(long PurID)
        {
            String sqlStr = @"SELECT 
	            Id,ProType,(SELECT ValueDic FROM DataDiction WHERE ID=ProType) Str_ProType,
	            ProAddress,(SELECT ValueDic FROM DataDiction WHERE ID=ProAddress) Str_ProAddress,
	            PurPrice,ShelfTime,ProCycle,DownTime,
	            Spec,(SELECT ValueDic FROM DataDiction WHERE ID=Spec) Str_Spec,Palate,
	            Grade,(SELECT ValueDic FROM DataDiction WHERE ID=Grade) Str_Grade,PurNum,
	            Varieti,FruitType,RetailPrice,Knowled
            FROM PruData WHERE Id='" + PurID + "'";

            IList<SelectPruDataTable3> SelPruData = BllBBCCommon.SelectEntityBySql<SelectPruDataTable3>("SelectPruDataTable3", sqlStr);
            ViewData["SelPruData3"] = SelPruData[0];
            return View();
        }

        public class SelectPruDataTable3
        {
            public long Id { get; set; }
            public int ProType { get; set; }
            public string Str_ProType { get; set; }
            public int ProAddress { get; set; }
            public string Str_ProAddress { get; set; }
            public decimal PurPrice { get; set; }
            public DateTime ShelfTime { get; set; }
            public string ProCycle { get; set; }
            public DateTime DownTime { get; set; }
            public int Spec { get; set; }
            public string Str_Spec { get; set; }
            public string Palate { get; set; }
            public int Grade { get; set; }
            public string Str_Grade { get; set; }
            public int PurNum { get; set; }
            public string Varieti { get; set; }
            public string FruitType { get; set; }
            public decimal RetailPrice { get; set; }
            public string Knowled { get; set; }
        }
    }
}